package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetMealMapper;
import com.itheima.reggie.service.SetMealService;
import com.itheima.reggie.service.SetmealDishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetMealServiceImpl extends ServiceImpl<SetMealMapper,Setmeal> implements SetMealService {
    @Autowired
    private SetmealDishService setmealDishService;

    //新增套餐同时保存套餐和菜品的关联关系
    @Override
    public void seaveWithDish(SetmealDto setmealDto) {
        //保存套餐的基本信息setmeal insert
        this.save(setmealDto);
        //菜品集合
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //保存套餐id
        setmealDishes.stream().map((item)->{
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        //保存套餐和菜品的关联信息setmeal_dish insert
        setmealDishService.saveBatch(setmealDishes);
    }
    //删除套餐以及和菜品的关联数据
    @Transactional
    @Override
    public void removeWithDish(List<Long> ids) {
        //查询套餐状态停售才能删除
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.in(Setmeal::getId,ids);
        queryWrapper.eq(Setmeal::getStatus,1);
        int count = this.count(queryWrapper);
        //不能删除，抛出业务异常
        if (count>0){
            throw new CustomException("套餐正在售卖中不能删除");
        }
        //可以删除，删除套餐数据
        this.removeByIds(ids);
        //再删关系表数据
        LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(SetmealDish::getSetmealId,ids);
        setmealDishService.remove(lambdaQueryWrapper);

    }
    @Override
    public void batchDeleteByIds(List<Long> ids) {
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(ids != null,Setmeal::getId,ids);

        List<Setmeal> list = this.list(queryWrapper);
        if (list != null){
            for (Setmeal setmeal : list) {
                if (setmeal.getStatus() == 0){
                    this.removeById(setmeal.getId());
                }else {
                    throw new CustomException("有套餐正在售卖，不能删除全部套餐！");
                }
            }
        }
    }

    @Override
    public SetmealDto getSetmealData(Long id) {
        Setmeal setmeal = this.getById(id);
        SetmealDto setmealDto = new SetmealDto();

        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(id != null,SetmealDish::getSetmealId,id);


        if (setmeal != null){
            BeanUtils.copyProperties(setmeal,setmealDto);

            List<SetmealDish> dishes = setmealDishService.list(queryWrapper);
            setmealDto.setSetmealDishes(dishes);

            return setmealDto;
        }

        return null;
    }


}
